<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *客户列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'客户渠道'),
);
$batch=array(
	array('type'=>'post','url'=>'/member.list/setstore/','title'=>'一键归属门店'),
);
$worker=pdo_get('jy_yxw_worker',array('id'=>$_SESSION['workerid'],'uniacid'=>$_SESSION['uniacid']));
	$storeidlist=str_array($worker['storeidlist']);
	if(!$storeidlist){
		$storeidlist=-1;
	}
//客户列表
if($_RQ['op']=='list'){
	
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'storeid'=>$storeidlist);
	if($_RQ['from_type']!='all'){
		$params['from_type']=intval($_RQ['from_type']);
	}
	$return['params']['from_type']=$_RQ['from_type'];
	if($_RQ['status']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:30;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$list=pdo_getslice('jy_yxw_member',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=$this->model('member')->member_show($item['id']);
			if($item['from_type']==1){
				$item['from_typetitle']=$this->member_from1_type[$item['from1_type']];
			}elseif($item['from_type']==2){
				$item['from_typetitle']=$this->member_from2_type[$item['from2_type']];
			}else{
				$item['from_typetitle']=$this->member_from_type[$item['from_type']];
			}
			
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','del_message'=>'确认删除该客户吗？','total'=>$total,'psize'=>$psize,'del_one'=>false,'delall'=>false,'edit'=>false,'html_to'=>'list_html','list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'storeid','title'=>'所属门店','type'=>'select2','options'=>'stores'),
			array('name'=>'jibie_type','title'=>'客户级别','type'=>'radio2','options'=>'jibie_type'),
			array('name'=>'from_typetitle','title'=>'渠道','type'=>'string'),
			array('title'=>'网络员','name'=>'worker3id','type'=>'select2','options'=>'workers'),
			array('title'=>'前台专员','name'=>'worker4id','type'=>'select2','options'=>'workers'),
			array('title'=>'电客员','name'=>'worker5id','type'=>'select2','options'=>'workers'),
			array('title'=>'销售员','name'=>'worker6id','type'=>'select2','options'=>'workers'),
			array('title'=>'体验员','name'=>'worker7id','type'=>'select2','options'=>'workers'),
			array('title'=>'试用员','name'=>'worker8id','type'=>'select2','options'=>'workers'),
			array('name'=>'status','title'=>'状态','type'=>'radio2','options'=>'status'),
			array('name'=>'createtime','title'=>'添加日期','type'=>'time'),
		),'batch'=>$batch),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'手机号','name'=>'mobile','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'客户名称','name'=>'title','help'=>''),
			array('name'=>'status','width'=>'15','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'storeid','width'=>'15','title'=>'所属门店','type'=>'select','options'=>'stores'),
		),
	);
	$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid'],'type'=>array(0,2)),array('id','title'),'id');
	$return['workers']=$this->getall('worker',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['status']=$this->member_status;
	$return['jibie_type']=$this->member_jibie_type;
	$return['from_type']=$this->member_from_type;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	exi('待开发中！！','error','list');
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_yxw_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$member=model('core/member')->detail($item['mid'],array('nickname','avatar','mobile','realname'));
		$item['nickname']=$member['nickname'];
		$item['avatar']=$member['avatar'];
		$item['mobile']=$member['mobile'];
		$item['realname']=$member['realname'];
	}else{
		$item=array();
	}
	$return['item']=$item;
	$return['item']['createtime']=date('Y-m-d H:i:s',$item['createtime']);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回客户列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'客户昵称','name'=>'nickname','help'=>''),
					array('type'=>'image','title'=>'头像','name'=>'avatar','help'=>''),
					array('type'=>'select','title'=>'客户组','name'=>'groupid','options'=>'group','help'=>''),
					array('type'=>'select','title'=>'客户等级','name'=>'level','options'=>'levels','help'=>''),
					array('type'=>'string','title'=>'真实姓名','name'=>'realname','help'=>''),
					array('type'=>'string','title'=>'手机号','name'=>'mobile','help'=>''),
					array('type'=>'string','title'=>'注册时间','name'=>'createtime','help'=>'','read'=>true),
					array('type'=>'switch','title'=>'加入黑名单','name'=>'isblack','help'=>'设置黑名单后，此客户无法访问商城'),
					
				)
			),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'groupid'=>intval($postdata['groupid']),
		'level'=>intval($postdata['level']),
		'isblack'=>intval($postdata['isblack']),
	);
	$member=array(
		'nickname'=>trim($postdata['nickname']),
		'avatar'=>trim($postdata['avatar']),
		'realname'=>trim($postdata['realname']),
	);
	$id=intval($postdata['id']);
	$mid=intval($postdata['mid']);
	if($id){
		pdo_update('jy_yxw_member',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_yxw_member',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='export'){
	//客户列表
	
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'storeid'=>$storeidlist);
	if($_RQ['id']){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	if(isset($_RQ['level_id'])){
		$params['level_id']=intval($_RQ['level_id']);
		$return['params']['level_id']=$_RQ['level_id'];
	}
	if(!empty($_RQ['nickname'])){
		$params['nickname like']='%'.$_RQ['nickname'].'%';
		$return['params']['nickname']=$_RQ['nickname'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if(!empty($_RQ['username'])){
		$params['username like']='%'.$_RQ['username'].'%';
		$return['params']['username']=$_RQ['username'];
	}
	if(!empty($_RQ['fromuser'])){
		$params['fromuser']=intval($_RQ['fromuser']);
		$return['params']['fromuser']=$_RQ['fromuser'];
	}
	
	$fields=array('id','avatar','nickname','username','mobile','credit1','credit2','createtime','fromuser');
	$list=pdo_getall('core_members',$params,$fields);
	if($list){
		foreach($list as &$item){
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
		}
	}
	$header=array(
		'id'=>'客户ID',
		'username'=>'客户账号',
		'nickname'=>'昵称',
		'mobile'=>'手机号',
		'credit1'=>'账户积分',
		'credit2'=>'账户余额',
		'createtime'=>'注册时间',
	);
	model('core/tools')->export2excel($header,$list,'客户数据');
}

if($_RQ['op']=='setstore'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$ids=$post['ids'];
		foreach($ids as $id){
			$id=intval($id);
			if($id){
				pdo_update('jy_yxw_member',array('storeid'=>intval($post['storeid'])),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			}
		}
		exi('操作成功！','','//list/');
	}
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
		$item=pdo_get('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),array('storeid','id'));
	}
	if(count($ids)==0){
		exi('请先选择客户','error');
	}
	$item['ids']=$ids;
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'绑定门店',
	);
	$htmls['edit']=array(
		array('type'=>'select','title'=>'绑定门店','name'=>'storeid','options'=>'stores'),
	);
	$return['htmls']=$htmls;
	$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));	
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_yxw_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}